using System;
using System.Linq;
using System.Collections.Generic;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Model.Layout;

namespace Kaleida.ServiceMonitor.Model.Runtime
{
    public class CompiledNode : IScriptNode
    {
        [CanBeNull]private readonly CompiledGroup scriptGroup;
        [CanBeNull]
        private readonly CompiledScriptModule scriptModule;

        public CompiledNode([NotNull] CompiledGroup scriptGroup)
        {
            if (scriptGroup == null) throw new ArgumentNullException("scriptGroup");
            this.scriptGroup = scriptGroup;
        }

        public CompiledNode([NotNull] CompiledScriptModule scriptModule)
        {
            if (scriptModule == null) throw new ArgumentNullException("scriptModule");
            this.scriptModule = scriptModule;
        }

        public bool IsLeaf
        {
            get { return scriptModule != null; }
        }

        public bool IsGroup
        {
            get { return scriptGroup != null; }
        }

        [NotNull]
        public CompiledGroup ScriptGroup
        {
            get
            {
                if (scriptGroup == null)
                    throw new InvalidOperationException("Node is not a Group");
                return scriptGroup;
            }
        }

        [NotNull]
        public CompiledScriptModule ScriptModule
        {
            get
            {
                if (scriptModule == null)
                    throw new InvalidOperationException("Node is not a ScriptModule");
                return scriptModule;
            }
        }

        IList<IScriptNode> IScriptNode.Children
        {
            get { return scriptGroup == null ? new List<IScriptNode>() : scriptGroup.Children.Cast<IScriptNode>().ToList(); }
        }
    }
}